﻿namespace AY38910
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;

    public abstract class FileReader
    {
        #region Public Methods and Operators

        public byte ReadByte(Stream stream)
        {
            return (byte)stream.ReadByte();
        }

        public byte[] ReadBytes(Stream stream, int count)
        {
            var bytes = new byte[count];
            stream.Read(bytes, 0, count);
            return bytes;
        }

        public string ReadString(BinaryReader reader)
        {
            var builder = new StringBuilder();
            while (true)
            {
                char charTemp = reader.ReadChar();
                if (charTemp == char.MinValue)
                {
                    break;
                }

                builder.Append(charTemp);
            }

            return builder.ToString();
        }

        /// <summary>
        ///     Reorders the bytes to get the proper endian
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public ushort ReadUInt16(BinaryReader reader)
        {
            return BitConverter.ToUInt16(reader.ReadBytes(2).Reverse().ToArray(), 0);
        }

        #endregion
    }
}